import VueRouter from 'vue-router'
import Home from '../pages/Home'
import Post from '../pages/Post'
import Echarts from '../pages/Echarts'
import Map from '../pages/Map'
import Login from '../pages/Login'
import Pages404 from '../pages/Pages404'
import Tree from '../pages/Tree'
import Cookies from 'js-cookie'

const router = new VueRouter({
    routes:[
        {
            path: '/Login',
            component: Login
        },
        {
            path: '/',
            redirect: '/home',
            component: Home,
            meta: { name: '首页' }
        },
        {
            path: '/home',
            component: Home,
            redirect: '/home/post',
            meta: { name: '首页' },
            children: [
                {
                    name: 'post',
                    path: 'post',
                    component: Post,
                    meta: { name: '表格展示' }
                },
                {
                    name: 'echarts',
                    path: 'echarts',
                    component: Echarts,
                    meta: { name: 'echarts图表' }
                },
                {
                    name: 'map',
                    path: 'map',
                    component: Map,
                    meta: { name: '地图' }
                },
                {
                    name: 'tree',
                    path: 'tree',
                    component: Tree,
                    meta: { name: '树形控件' }
                }
            ]
        },
        {
            path: '/pages404',
            component: Pages404
        },
        {
            path: '*',
            redirect: '/pages404'
        }
    ]
})
router.beforeEach((to,from,next)=>{
    const name = Cookies.get('name')
    if(name){
        if(to.path == '/login'){
            next('/home')
        }else{
            next()
        }
    }else {
        if(to.path == '/pages404'){
            next()
        }else if(to.path == '/login'){
            next()
        }else{
            next('/login')
        }
    }
})

export default router